define("login", ["jquery", "md5", "jquery.validate", "jquery.idcode"], function() {
	return {
		login: function() {

			$.idcode.setCode(); //加载生成验证码方法
			$("form").validate({
				rules: {
					uname: {
						required: true
					},
					upwd: {
						required: true
					},
					Txtidcode: {
						required: true
					}
				},
				messages: {
					uname: {
						required: "请输入用户名"
					},
					upwd: {
						required: "请输入密码"
					},
					Txtidcode: {
						required: ""
					}
				},
				submitHandler: function() {
					var formdata = new FormData();
					var $data = $("form").serializeArray();
					$data.forEach((ele, index) => {
						var stats = ele.name == "upwd";
						formdata.append(ele.name, stats ? $.md5(ele.value) : ele.value);
					})

					$.ajax({
						url: "./../server/login.php",
						type: "post",
						dataType: "json",
						data: formdata,
						processData: false,
						contentType: false
					}).then(function(res) {

						if(res.status == 1) {

							alert("登录成功!");
							console.log(res)
							window.location = "index.html"
						} else {
							alert("账号或密码错误")
						}

					})

					return false;

				},
				errorPlacement: function(error, ele) {

					error.appendTo($("#msg"))

				}

			})

		}
	}
})